'use strict';

$(function(){
    obj.init();
});

var obj = new JsFun();
function JsFun(){
    return {
        params: {
            mac: null,
            explain: {
                id: '#',
                boxMac : '盒子MAC',
                retyTime : '重试次数',
                timestamp : '时间',
                boxInfoId: '盒子id',
                boxMd5: '盒子md5',
                downloadSpeed: '平均下载速度',
                fileCount: '资源总数',
                fileSizeCount: '资源总大小',
                timeBegin: '开始时间',
                timeEnd: '结束时间',
                duration: '淘汰用时',
                type: '类型',
                reduced: '缩减空间',
                left: '磁盘剩余空间',
                expectMd5: '获取md5',
                realMd5: '正确md5',
                fileMd5: '资源md5',
                fileUrl: '资源地址',
                realTime: '估算用时',
                expectTime: '实际用时',
                hits: '命中次数',
            },
            exclude: [
                'boxInfoId',
                'boxMd5'
            ],
            roles: {
                fileSizeCount: '(${value}/1024).toFixed(2) + "MB"',
                downloadSpeed: '${value} + "KB/秒"',
                realTime: '${value} + "秒"',
                expectTime: '${value} + "秒"',
                duration: '${value} + "秒"',
                reduced: '(${value}/1024/1024).toFixed(2) + "MB"',
                left: '${value} + "MB"',
                type: '${value} === 0 ? "应用" : (${value} === 1 ? "题库" : "其他")',
            }
        },
        dom: {
            stateDom: null,
            table: null,
            tableHead: null,
            tableBody: null,
            page: null,
            size: null
        },
        init: function () {
            this.params.mac     = xhTool.getQuery('mac');
            this.dom.stateDom   = $('#ul_state').find('a');
            this.dom.table      = $('#tb_state');
            this.dom.tableHead  = this.dom.table.find('thead > tr');
            this.dom.tableBody  = this.dom.table.find('tbody');
            this.dom.page       = $('#page');
            this.dom.size       = $('#size');
            this.listener();
        },
        listener: function () {
            var $this = this;
            //选择状态
            $this.dom.stateDom.click(function(){
                $this.funObj().stateSelect($(this).attr('href').substr(1));
            });
        },
        funObj: function(){
            var $this = this;
            return {
                stateSelect: function(_target) {
                    $this.dom.tableHead.html('<th>请求中...请稍后...</th>');
                    $this.dom.tableBody.html('');
                    xhExtra.ajaxRequest('Server/getBoxState', {mac: $this.params.mac, target: _target, page: $this.dom.page.val(), size: $this.dom.size.val()}, {}, function(result){
                        if (result.length <= 0){
                            $this.dom.tableHead.html('<th>无数据！</th>');
                            return false;
                        }
                        //处理表头
                        var headStr = '';
                        $.each(result[0], function (key, value) {
                            if ($.inArray(key, $this.params.exclude) >= 0) {
                                return true;
                            }
                            headStr += '<th>' + (typeof $this.params.explain[key] === 'undefined' ? key : $this.params.explain[key])  + '</th>'
                        });
                        //处理表内容
                        var bodyStr = '';
                        $.each(result, function (index, row) {
                            bodyStr += '<tr>';
                            $.each(row, function (key, value) {
                                //去除排除项
                                if ($.inArray(key, $this.params.exclude) >= 0) {
                                    return true;
                                }
                                //显示更改
                                if (typeof $this.params.roles[key] === 'string') {
                                    var temp = $this.params.roles[key].replace(/\${value}/g, value);
                                    value = eval($this.params.roles[key].replace(/\${value}/g, value));
                                }
                                bodyStr += '<td>' + value + '</td>'
                            });
                            bodyStr += '</tr>';
                        });
                        $this.dom.tableHead.html(headStr);
                        $this.dom.tableBody.html(bodyStr);
                    });
                }
            }
        },
    }
}